package com.apes.fn.scm.retailOrder.service;

import com.apes.fn.scm.retailOrder.model.BudgetCut;
import com.apes.fn.scm.retailOrder.repository.BudgetCutRepository;
import com.apes.framework.api.DomainService;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 预算提成
 * @author zzx
 * @create 2020年9月8日09:14:16
 */
@Service("budgetCutService")
public class BudgetCutService extends DomainService {

    @Autowired
    private BudgetCutRepository budgetCutRepository;

    /***
     * 功能：保存
     * @param request
     * @return
     */
    public BudgetCut sava(SimpleRequest request){
        BudgetCut budgetCut = request.getO(BudgetCut.class);
        return budgetCutRepository.saveAndFlush(budgetCut);
    }

    /**
     * 功能：修改
     * @param request
     * @return
     */
    public BudgetCut update(SimpleRequest request){
        String id = request.get("id");
        BudgetCut budgetCut = request.getO(BudgetCut.class);
        BudgetCut tempBudgetCut = budgetCutRepository.findOne(id);
        if (tempBudgetCut == null) throw new RuntimeException("状态" + id + "】在系统中不存在。");
        if(tempBudgetCut.getVersion() != budgetCut.getVersion()) throw new RuntimeException("记录已更新或已删除，请重新提交！");  // 用于判断当前版本是否被修改
        return budgetCutRepository.saveAndFlush(budgetCut);
    }

    /***
     * 功能：删除
     * @param request
     * @return
     */
    public String delete(SimpleRequest request){
        String id = request.get("id");
        BudgetCut budgetCut = request.getO(BudgetCut.class);
        BudgetCut tempBudgetCut = budgetCutRepository.findOne(id);
        if (tempBudgetCut == null) throw new RuntimeException("状态" + id + "】在系统中不存在。");
        if(tempBudgetCut.getVersion() != budgetCut.getVersion()) throw new RuntimeException("记录已更新或已删除，请重新提交！");
        budgetCutRepository.delete(budgetCut);
        return "删除成功";
    }

}
